package com.study.jvm;

import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.Opcodes;

/**
 * -XX:MaxMetaspaceSize=8m
 */
//java.lang.OutOfMemoryError: Compressed class space
public class MethodAreaOom extends ClassLoader {
    public static void main(String[] args) {
        int count = 0;
        try {
            MethodAreaOom methodAreaOom = new MethodAreaOom();
            for (int i = 0; i < 1000000; i++) {
                ClassWriter cw = new ClassWriter(0);
                cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, "Class_Oom" + i, null, "java/lang/Object", null);
                byte[] bytes = cw.toByteArray();
                methodAreaOom.defineClass("Class_Oom" + i, bytes, 0, bytes.length);
                count += 1;
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("count = " + count);
        }

    }
}
